package com.socialuni.center.web.serive.app;

import com.socialuni.center.web.entity.ThirdUserLoginEntity;
import com.socialuni.center.web.manage.DevAccountDOCheck;
import com.socialuni.center.web.manage.ThirdUserTokenDOCreate;
import com.socialuni.center.web.model.DO.oAuth.ThirdUserAuthDO;
import com.socialuni.center.web.model.DO.oAuth.ThirdUserDO;
import com.socialuni.center.web.model.DO.oAuth.ThirdUserTokenDO;
import com.socialuni.center.web.model.DO.user.DevAccountDO;
import com.socialuni.center.web.model.EO.ThirdUserLoginEO;
import com.socialuni.center.web.repository.oAuth.ThirdUserAuthRepository;
import com.socialuni.center.web.utils.CenterUserUtil;
import com.socialuni.center.web.utils.DevAccountUtils;
import com.socialuni.center.web.utils.model.UserOAuthROUtil;
import com.socialuni.sdk.constant.AuthType;
import com.socialuni.sdk.constant.CommonStatus;
import com.socialuni.sdk.exception.SocialBusinessException;
import com.socialuni.sdk.exception.SocialNullUserException;
import com.socialuni.sdk.model.DO.user.SocialPhoneNumDO;
import com.socialuni.sdk.model.DO.user.UserDO;
import com.socialuni.sdk.model.QO.OAuthUserInfoQO;
import com.socialuni.sdk.model.RO.OAuthUserPhoneNumRO;
import com.socialuni.sdk.model.RO.SocialOAuthUserRO;
import com.socialuni.sdk.model.RO.UniLoginRO;
import com.socialuni.sdk.store.SocialUserPhoneStore;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

//授权用户信息
@Service
public class OAuthService {
    @Resource
    ThirdUserTokenDOCreate thirdUserTokenDOCreate;
    @Resource
    DevAccountDOCheck devAccountDOCheck;
    @Resource
    SocialUserPhoneStore socialUserPhoneStore;
    //默认会根据微信的appid去生成不太对，应该用咱们系统的appid
    //为什么需要appid和渠道类型，因为需要去判断获取对应的开发者账户
    @Resource
    ThirdUserLoginEntity thirdUserLoginEntity;

    public UniLoginRO<SocialOAuthUserRO> oAuthUserInfo(OAuthUserInfoQO authVO) {
        return this.oAuthUserInfo(authVO, AuthType.user);
    }

    public UniLoginRO<SocialOAuthUserRO> oAuthUserPhoneNum(OAuthUserInfoQO authVO) {
        return this.oAuthUserInfo(authVO, AuthType.phone);
    }

    public UniLoginRO<SocialOAuthUserRO> oAuthUserInfo(OAuthUserInfoQO authVO, String authType) {
        //获取devaccount
        //获取绑定的手机号，根据手机号，获取user。然后返回这个user的信息，并且返回对应这个应用的userid
        UserDO userDO = CenterUserUtil.getMineUser();

        return oAuthUserInfo(authVO, userDO, authType);
    }

    //授权获取用户信息，根据appId知道授权给哪个商户的
    public UniLoginRO<SocialOAuthUserRO> oAuthUserInfo(OAuthUserInfoQO authVO, UserDO userDO, String authType) {
        //获取开发者对应的账号
        DevAccountDO devAccountDO = devAccountDOCheck.checkApplyAuthQO(authVO);

        ThirdUserLoginEO thirdUserLoginEO = thirdUserLoginEntity.thirdUserLogin(devAccountDO, userDO, authType);

        //只是记录一个授权记录
        ThirdUserDO threeUserDORO = thirdUserLoginEO.getThreeUserDO();
        //授权成功
        //token有效期三个月
        //生成userToken

        ThirdUserTokenDO tokenDO = thirdUserLoginEO.getTokenDO();

        SocialOAuthUserRO authUserContentRO = UserOAuthROUtil.newUserOAuthROByThreeUser(threeUserDORO, userDO);

        //只有为手机号授权时才返回手机号
        if (authType.equals(AuthType.phone)) {
            SocialPhoneNumDO socialUserPhoneDO = socialUserPhoneStore.findByUserId(userDO.getId());
            if (socialUserPhoneDO == null) {
                throw new SocialBusinessException("用户未绑定手机号，请先绑定手机号");
            }
            authUserContentRO.setPhoneNum(socialUserPhoneDO.getPhoneNum());
        }

        //生成返回对象
        UniLoginRO<SocialOAuthUserRO> applySocialUniOAuthRO = new UniLoginRO<>();

        applySocialUniOAuthRO.setToken(tokenDO.getToken());
        applySocialUniOAuthRO.setUser(authUserContentRO);

        return applySocialUniOAuthRO;
    }


    @Resource
    private ThirdUserAuthRepository thirdUserAuthRepository;

    public OAuthUserPhoneNumRO getUserPhoneNum() {
        UserDO userDO = CenterUserUtil.getMineUser();
        DevAccountDO devAccountDO = DevAccountUtils.getDevAccount();
        ThirdUserAuthDO threeUserAuthDOOptional = thirdUserAuthRepository.findByDevIdAndUserIdAndAuthTypeAndStatus(devAccountDO.getId(), userDO.getId(), AuthType.phone, CommonStatus.enable);
        //授权过
        if (threeUserAuthDOOptional == null) {
            throw new SocialBusinessException("用户未授权");
        }

        SocialPhoneNumDO socialUserPhoneDO = socialUserPhoneStore.findByUserId(userDO.getId());
        if (socialUserPhoneDO == null) {
            throw new SocialNullUserException();
        }
        OAuthUserPhoneNumRO oAuthUserPhoneNumRO = new OAuthUserPhoneNumRO(socialUserPhoneDO.getPhoneNum());

        return oAuthUserPhoneNumRO;
    }
}
